package com.linux.huhx.codetools.generate;

import com.linux.huhx.codetools.transutil.CodeToolUtils;

import static com.linux.huhx.utils.RegrexUtils.*;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.test.context.TestExecutionListeners;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.*;

/**
 * Created by huhx on 2017-06-15.
 */
public class GernerateJava {
    public final static String FILE_NAME_PATH = "G:/Java/JavaEE/Program/2016-05-15/SpringLearn/huhx-test/target/classes/com/file/huhx.txt";
    public final static String FILE_NAME_PATH2 = "C:/Users/76801/Desktop/huhx.txt";

    @Test
    public void genreateService() {
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String requestString = "package com.csii.mca.datamodel.qrcode;\n\npublic class " + string + "Request {\n\n}";
                String responseString = "package com.csii.mca.datamodel.qrcode;\n\npublic class " + string + "Response {\n\n}";
                String serviceString = "package com.csii.mca.service.qrcode;\n\nimport com.csii.mca.datamodel.qrcode." + string + "Request;\n" +
                        "import com.csii.mca.datamodel.qrcode." + string + "Response;\nimport com.csii.pe.dynamic.service.QueryService;\n\n" +
                        "public interface " + string + "Service extends QueryService<" + string + "Request, " + string + "Response> {\n\n}";
                File file1 = new File("D:/huhxfile/request", string + "Request.java");
                FileUtils.write(file1, requestString);
                File file2 = new File("D:/huhxfile/request", string + "Response.java");
                FileUtils.write(file2, responseString);
                File file3 = new File("D:/huhxfile/service", string + "Service.java");
                FileUtils.write(file3, serviceString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generate_2() {
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String line = "<bean id=\"" + string + "Service\" class=\"com.csii.mca.service.qrcode.impl." + string + "ServiceImpl\">\n" +
                        "\t<ref name=\"qrCodeService\">.qrcode.QRCodeService</ref>\n" +
                        "</bean>\n";
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void gernerate_3() {
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String line = "package com.csii.mca.service.qrcode.impl;\n\npublic class " + string + "ServiceImpl extends MCQueryService<" + string + "Request, " +
                        "" + string + "Response> implements " + string + "Service {\n\n" +
                        "private QRCodeService qrCodeService;\n\npublic QRCodeService getQrCodeService() {\nreturn qrCodeService;\n}public void setQrCodeService(QRCodeService qrCodeService) {this.qrCodeService = qrCodeService;}\n\n" +
                        "@Override\npublic " + string + "Response query(" + string + "Request request) {\n\n}";
                File file1 = new File("D:/huhxfile/impl", string + "ServiceImpl.java");
                FileUtils.write(file1, line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void batchRenameFiles() {
        File[] files = new File("D:\\file\\test").listFiles();
        for (File file : files) {
            file.renameTo(new File("d:/file/test/huhx", "MC" + file.getName()));
        }
    }

    @Test
    public void generateCode_1() {
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String requestString = "<osgi:service ref=\"qrcode." + string + "Service\" interface=\"com.csii.pe.dynamic.service.QueryService\" />";
                System.out.println(requestString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 产生请求和响应报文的基础类
    private void generateDatagram(String requestDatagram) {
        File outputFile = new File("C:/Users/76801/Desktop/output.txt");
        File file = new File(FILE_NAME_PATH2);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            List<String> resultLists = new ArrayList<>();
            for (String string : listStrings) {
                String line = MessageFormat.format(requestDatagram, string, CodeToolUtils.transforFirstLetterUpper(string));
                resultLists.add(line);
            }
            FileUtils.writeLines(outputFile, resultLists);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 请求报文
    @Test
    public void generateDatagram_1() {
        String requestDatagram = "<xmlTag tagName=\"{0}\"><String name=\"{1}\"></String></xmlTag>";
        generateDatagram(requestDatagram);
    }

    // 响应报文
    @Test
    public void generateDatagram_2() {
        String requestDatagram = "<elementAlias name=\"{0}\" alias=\"{1}\"></elementAlias>";
        generateDatagram(requestDatagram);
    }

    // 一列的数据，第一列是名字
    @Test
    public void generate_javaBean1() {
        String requestDatagram = "private String {0};";
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String line = MessageFormat.format(requestDatagram, string);
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 两列的数据，第一列是名字，第二列是注释
    @Test
    public void generate_javaBean2() {
        String requestDatagram = "private String {0}; // {1}";
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] strings = string.split("\t"); // 这个分割符号可以自行修改
                String line = MessageFormat.format(requestDatagram, strings[1], strings[0]); // 这里面:注释、名字
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成权限的代码
    @Test
    public void generate_trs_1() {
        String requestData = "<prop key=\"{0}.do\">BaseTwophaseAction</prop>";
        String roleData = "<entry key=\"{0}\" value=\"{1}\"/>";
        File file = new File("C:\\Users\\76801\\Desktop\\huhx2.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            // 处理dispatche的trs
            for (String string : listStrings) {
                dealWithAddTrs(requestData, string);
            }
            System.out.println("************************************************************");
            // 处理dispatche的role
            for (String string : listStrings) {
                dealWithAddRole(roleData, string);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generate_action_1() {
        String request = "<bean id=\"{0}Service\" class=\"com.cmb.minapp.mweb.business.{1}.service.{2}Service\">";
        File file = new File("C:/Users/76801/Desktop/huhx.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            // 处理dispatche的trs
            for (String string : listStrings) {
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void dealWithAddRole(String roleData, String string) {
        List<String> listStrings = new ArrayList<>();
        listStrings.add("AddConfirm");
        listStrings.add("Add");
        listStrings.add("ModifyConfirm");
        listStrings.add("Modify");
        listStrings.add("DelConfirm");
        listStrings.add("Del");

        for (String listString : listStrings) {
            System.out.println(MessageFormat.format(roleData, string + listString, string + listString.replace("Confirm", "")));
        }
        System.out.println("--------------------------------------------------------------");
    }

    private void dealWithAddTrs(String request, String string) {
        List<String> listStrings = new ArrayList<>();
        listStrings.add("AddConfirm");
        listStrings.add("Add");
        listStrings.add("ModifyConfirm");
        listStrings.add("Modify");
        listStrings.add("DelConfirm");
        listStrings.add("Del");

        for (String listString : listStrings) {
            System.out.println(MessageFormat.format(request, string + listString));
        }
        System.out.println("--------------------------------------------------------------");
    }

    // 生成js中的scope的语句
    @Test
    public void generate_scope_1() {
        String requestDatagram = "$scope.{0} = info.{0};";
        File file = new File("C:/Users/76801/Desktop/huhx2.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                string = string.split(" ")[0];
                System.out.println(MessageFormat.format(requestDatagram, string));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成postdata的语句
    @Test
    public void generate_postData_1() {
        String requestData = "\t\"{0}\": $scope.{0},";
        File file = new File("C:/Users/76801/Desktop/huhx2.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            System.out.println("var postData = {");
            for (String string : listStrings) {
                string = string.split(" ")[0];
                System.out.println(MessageFormat.format(requestData, string));
            }
            System.out.println("};");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成前端的页面
    @Test
    public void generate_htmlpage_1() {
        File file = new File("C:/Users/76801/Desktop/huhx.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                string = string.split(" ")[0];
                String requestData = "<div class=\"m-input-row\">\n" +
                        "\t<div class=\"u-row-title\">商户类型：</div>\n" +
                        "\t<div class=\"u-row-text\">{{" + string + "}}</div>\n" +
                        "</div>";
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generate_querytrs_1() {
        File file = new File("C:/Users/76801/Desktop/liuling.txt");
        String trs_request = "<prop key=\"{0}.do\">{0}Action</prop> <!-- {1} -->";
        String role_request = "<entry key=\"{0}\" value=\"{0}\" /> <!-- {1} -->";
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = MessageFormat.format(trs_request, arrays[0], arrays[1]);
                System.out.println(requestData);
            }
            System.out.println("----------------------------------------------");
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = MessageFormat.format(role_request, arrays[0], arrays[1]);
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void generateDefined_action() {
        File file = new File("C:/Users/76801/Desktop/liuling.txt");
        String action = "    <!-- {1} -->\n" +
                "    <bean id=\"{0}Action\" class=\"com.cmb.minapp.mweb.business.statistics.action.{0}Action\">\n" +
                "\t\t<property name=\"dbService\" ref=\"dbService\"/>\n" +
                "\t</bean>\n";
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = MessageFormat.format(action, arrays[0], arrays[1]);
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // dao中的语句书写查询
    @Test
    public void generate_query_dao() {
        File file = new File("C:/Users/76801/Desktop/liuling.txt");
        String action = "// {1}\n" +
                "public List<Map> {0}(Map request) {\n" +
                "\treturn dbService.selectList(\"statistics.{0}\", request);\n" +
                "}\n";
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = messageFormat(action, CodeToolUtils.transforFirstLetterLower(arrays[0]), arrays[1]);
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // sql语句的select
    @Test
    public void generate_query_sql() {
        File file = new File("C:/Users/76801/Desktop/liuling.txt");
        String action = "<!-- {1}  -->\n" +
                "<select id=\"{0}\" resultType=\"java.util.HashMap\" parameterType=\"java.util.HashMap\">\n" +
                "\t\n" +
                "</select>\n";
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = messageFormat(action, CodeToolUtils.transforFirstLetterLower(arrays[0]), arrays[1]);
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void filesCopyTest() throws IOException {
        Path srcPath = new File("C:/Users/76801/Desktop/liuling.txt").toPath();
        Path destPath = new File("C:/Users/76801/Desktop/LL.txt").toPath();
        long beforeCopy = System.currentTimeMillis();
        Files.copy(srcPath, destPath);
        long afterCopy = System.currentTimeMillis();
        System.out.println("cost time " + (afterCopy - beforeCopy));
    }

    @Test
    public void properties_fileTest() throws Exception {
        Properties properties = new Properties();
        InputStream inputStream = new FileInputStream("E:\\data\\git\\project\\csii\\imp-test\\minapp-cmb\\minapp-cmb-server\\minapp-config\\src\\main\\resources\\config\\msg\\checkmsg.properties");
        properties.load(inputStream);

        String username = properties.getProperty("validation.length");
        System.out.println(new String(username.getBytes("iso-8859-1"), "gbk"));
        System.out.println(new String(username.getBytes("US-ASCII"), "UTF-8"));
        System.out.println(new String(username.getBytes("utf-8"), "gbk"));
        System.out.println(username);
    }

    @Test
    public void base64() {
//        String string = "WWdV04qQE2QEGoGVWPxi+JFJsTGOqWPR062qAqauoi/Iwcw+oTbQnwkbWP8azMT08otdgtm0YBc+shLNGnWztDlE7jnVPVD6Yu18yjQLntUVSRmuiq3jsHY2kRVi7aHOuIwGJd4jdC+gO0eUw++E1TUHjRAlHAayF9p3q9CrhN8XJ9ixtQ5VenPAtsH8q2oaoBTQ/OQxR2WBvznm1FTsiuzxmbCKsWEiD7Qr44y324YNENmTN0VrjvJ1cu+QRI7SH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Aw9Gk8JUrOy4lhcDIiq4L0kpefnTbVBrdzNLCgSgQ064xiRnXlzivAmKfcwmYkfzY0eStLeH/m9+MXOdUgCHAwd8dQ83xRlzQcu7xZKZy/Sw00lrdBcn1wEuqpMkc1tkLMHIXcNg6K5uSBkK7JV8AttjMiDyGpjz3MZGrIFLx/PmG3uvIWYzBje5mk803VEWq4m6bdE8L";
        String string = "10aaDSJCZeIe4yoK3KujBQ==";
        String str = new String(Base64.decodeBase64(string));
        System.out.println(str);
    }


    @Test
    public void genreate_java_html() {
        String action = "<div class=\"m-input-row\">\n" +
                "\t<div class=\"u-row-title\">{1}</div>\n" +
                "\t<div class=\"u-row-text\">{{{0}}}</div>\n" +
                "</div>";
        File file = new File("C:/Users/76801/Desktop/huhx2.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                String[] arrays = string.split(" ");
                String requestData = messageFormat(action, arrays[0], arrays[1]);
                System.out.println(requestData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
